﻿using SuperMap.Data;
using SuperMap.Realspace;
using SuperMap.UI;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using Ycjw.PipeLine.Map.Basic.SuperMap;
using Ycjw.PipeLine.Scenes.Views;

namespace Ycjw.PipeLine.Scenes
{
    /****************************************************************************************/
    /*
    功    能:放置三维场景的DocWindow
    作    者: RenZachary
    联系方式: rzcgis@foxmail.com（QQ455745253）
    生成日期: 2021年3月24日
    修改日志: 
    版权说明: @2021 西安云创经纬信息科技有限公司 版权所有 All right reserved.
    注    意: 本内容仅限于云创经纬公司内部使用，禁止用于商业目的。
    */
    /****************************************************************************************/
    class SceneEntitySelect :SceneOperInterface
    {
        private Selection3D m_Selection;
        private BubbleDefault m_BubbleDefault;

        private String m_BubbleTitle = "";
        public String BubbleTitle
        {
            get { return m_BubbleTitle; }
            set { m_BubbleTitle = value; }
        }

        private Int32 m_FrameWidth = 2;
        public Int32 BubbleFrameWidth
        {
            get { return m_FrameWidth; }
            set { m_FrameWidth = value; }
        }

        private Color m_BackColor = Color.FromArgb(238, 238, 242);
        public Color BackColor
        {
            get { return m_BackColor; }
            set { m_BackColor = value; }
        }

        private Int32 m_RoundQuality = 20;
        public Int32 RoundQuality
        {
            get { return m_RoundQuality; }
            set { m_RoundQuality = value; }
        }

        private Boolean m_IsHide;
        public Boolean IsHide
        {
            get { return m_IsHide; }
            set { m_IsHide = value; }
        }

        private TextStyle m_TitleStyle;
        public TextStyle TitleStyle
        {
            get { return m_TitleStyle; }
            set { m_TitleStyle = value; }
        }

        /// <summary>
        /// 根据workspace和m_SceneControl构造 SampleRun对象
        /// Initialize the SampleRun object with the specified workspace and m_SceneControl
        /// </summary>
        public SceneEntitySelect( SceneControl sceneControl,FormMainRibbon mainForm):base(sceneControl, mainForm)
        {
            try
            {
                m_SceneControl = sceneControl;
                m_FormMain = mainForm;

                Initialize();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        public override void UnRegister() 
        {
            m_UnRegistered = true;
        }
        /// <summary>
        /// 打开需要的工作空间文件及场景
        /// Open the workspace and the scene
        /// </summary>
        private void Initialize()
        {
            try
            {
                m_BubbleDefault = new BubbleDefault()
                {
                    Visible = false
                };
                m_SceneControl.Controls.Add(m_BubbleDefault);
                // 调整m_SceneControl的状态
                // Adjust the status of m_SceneControl
                m_SceneControl.Action = Action3D.Select;

                m_SceneControl.ObjectSelected += new ObjectSelectedEventHandler(SceneControl_ObjectSelected);
                m_SceneControl.MouseDown += new MouseEventHandler(SceneControl_MouseDown);
                m_SceneControl.BubbleResize += new BubbleResizeEventHandler(SceneControl_BubbleResize);
                m_SceneControl.BubbleClose += new BubbleCloseEventHandler(SceneControl_BubbleClose);
                m_SceneControl.BubbleInitialize += new BubbleInitializeEventHandler(SceneControl_BubbleInitialize);

                ((FormMainRibbon)m_FormMain).ribbonButtonSelect.Click += RibbonButtonSelect_Click;

                m_TitleStyle = new TextStyle();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        private void RibbonButtonSelect_Click(object sender, EventArgs e)
        {
            this.m_UnRegistered = false;
        }


        /// <summary>
        /// 关闭气泡时，触发的事件
        /// Trigger the event when you close the bubble
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SceneControl_BubbleClose(object sender, BubbleEventArgs e)
        {
            // 关闭气泡
            // Close the bubble
            m_BubbleDefault.Visible = false;
        }

        /// <summary>
        /// 气泡改变时，触发的事件
        /// Trigger the event when you change the bubble
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SceneControl_BubbleResize(object sender, BubbleEventArgs e)
        {
            if (m_UnRegistered) return;
            // 选择不同的地物弹出不同的气泡
            // Different features have different bubbles
            System.Drawing.Point point = new Point(e.Bubble.ClientLeft, e.Bubble.ClientTop);
            if (m_Selection != null)
            {
                m_BubbleDefault.Location = point;
                m_BubbleDefault.Visible = true;
            }
        }

        /// <summary>
        /// 气泡初始化时，触发的事件
        /// Tigger the event if you initialize the bubble
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SceneControl_BubbleInitialize(object sender, BubbleEventArgs e)
        {
            if (m_UnRegistered) return;
            // 选择不同的地物弹出不同的气泡
            // Different features have different bubbles
            System.Drawing.Point point = new Point(e.Bubble.ClientLeft, e.Bubble.ClientTop);
            if (m_Selection != null)
            {
                m_BubbleDefault.Location = point;
            }
        }

        /// <summary>
        /// 鼠标右键，销毁气泡和清除选择集
        /// Context menu, remove the bubble
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SceneControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (m_UnRegistered) return;
            try
            {
                if (e.Button == MouseButtons.Right)
                {
                    if (m_Selection == null) return;
                    if (m_Selection.Count > 0)
                    {
                        m_Selection.Clear();
                    }
                    if (m_SceneControl.Bubbles.Count > 0)
                    {
                        m_SceneControl.Bubbles.Clear();
                    }
                }
            }
            catch
            {
            }
        }

        /// <summary>
        /// 选中模型或点对象时弹出气泡
        /// Pop up the bubble if you select a model or a point
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SceneControl_ObjectSelected(object sender, ObjectSelectedEventArgs e)
        {
            if (m_UnRegistered) return;
            Recordset recordset = null;
            try
            {
                if (m_SceneControl.Bubbles.Count > 0)
                {
                    m_SceneControl.Bubbles.Clear();
                }
                if (m_SceneControl.Scene.FindSelection(true)[0] != null)
                {
                    m_Selection = m_SceneControl.Scene.FindSelection(true)[0];
                    recordset = m_Selection.ToRecordset();
                    Bubble bubble = null;


                    Layer3DDataset layer3DDataset = m_Selection.Layer as Layer3DDataset;
                    ///建筑物模型数据不显示属性信息
                    if (layer3DDataset.Dataset != null && layer3DDataset.Dataset.Type == DatasetType.Model) return;
                    ///树木等小品不显示属性信息
                    if (layer3DDataset.Dataset.Datasource.Alias == "Others") return;
                    bubble = new Bubble() { 
                        BackColor = Color.FromArgb(255, 6, 31, 48),
                        FrameColor = Color.FromArgb(255, 6, 31, 48)
                    };
                    m_SceneControl.Bubbles.Add(bubble);
                    Point3D point3D = new Point3D(recordset.GetGeometry().InnerPoint.X,
                        recordset.GetGeometry().InnerPoint.Y, 0);
                    if(recordset.GetGeometry() is Geometry3D)
                    {
                        point3D.Z = (recordset.GetGeometry() as Geometry3D).InnerPoint3D.Z;
                    }

                    // 设置气泡指向点
                    // Set bubble point
                    bubble.Pointer = point3D;
                    // 设置气泡标题
                    // Set bubble title
                    bubble.Title = m_BubbleTitle;
                    // 设置气泡圆角的程度
                    // Set round quality
                    bubble.RoundQuality = m_RoundQuality;
                    // 设置气泡是否自动隐藏
                    // Set whether to hide the bubble or not
                    bubble.IsAutoHide = m_IsHide;
                    // 设置气泡边框宽度
                    // Set frame width
                    bubble.FrameWidth = m_FrameWidth;
                    bubble.TitleTextStyle = new TextStyle() { AlignmentString = SuperMap.Data.StringAlignment.Center };
                    // 设置气泡边框背景
                    // Set background color
                    bubble.BackColor = m_BackColor;
                    // 设置气泡标题文本风格
                    // Set title text style
                    bubble.TitleTextStyle = m_TitleStyle;
                    FillBubbleControlModel(recordset);
                    bubble.ClientWidth = m_BubbleDefault.Width;
                    bubble.ClientHeight = m_BubbleDefault.Height;
                }
            }
            catch (System.Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
            finally
            {
                if (recordset != null)
                {
                    recordset.Close();
                    recordset.Dispose();
                }
            }
        }

        /// <summary>
        /// 填充特殊模型气泡上的信息
        /// Fill the information to the special model bubble
        /// </summary>
        private void FillBubbleControlModel(Recordset recordset)
        {
            // 存放特殊模型临近点的记录集
            // Store the neighbor recordset of the special model
            Recordset recordsetNeighbor1 = null;
            Recordset recordsetNeighbor2 = null;
            try
            {
                m_BubbleDefault.Clear();
                var fieldInfos = recordset.GetFieldInfos();
                for (int i = 0;i < recordset.FieldCount;i++)
                {
                    var caption = fieldInfos[i].Caption;
                    var fieldName = fieldInfos[i].Name;
                    if (fieldName.ToUpper().StartsWith("SM") && fieldName.ToUpper() != "SMID") continue;
                    var valueObject = recordset.GetFieldValue(fieldName);
                    var value = valueObject == null ? string.Empty : valueObject.ToString();
                    m_BubbleDefault.Add(caption, value);
                }

                m_BubbleDefault.Visible = true;

            }
            catch (System.Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
            finally
            {
                if (recordsetNeighbor1 != null)
                {
                    recordsetNeighbor1.Close();
                    recordsetNeighbor1.Dispose();
                }
                if (recordsetNeighbor2 != null)
                {
                    recordsetNeighbor2.Close();
                    recordsetNeighbor2.Dispose();
                }
            }
        }
    }
}
